package com.one.efaimaly.main.ui.fragment;

import android.view.View;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.Subscribe;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.utils.map.LatLng;
import com.one.efaimaly.utils.LocationUtils;
import com.one.common.utils.map.MapUtils;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListFragment;
import com.one.efaimaly.R;
import com.one.efaimaly.main.model.event.SelectCityEvent;
import com.one.efaimaly.main.model.item.BannerItem;
import com.one.efaimaly.main.model.item.OrderEmptyItem;
import com.one.efaimaly.main.model.item.OrderItem;
import com.one.efaimaly.main.presenter.MainPresenter;
import com.one.efaimaly.main.ui.binder.BannerBinder;
import com.one.efaimaly.main.ui.binder.OrderBinder;
import com.one.efaimaly.main.ui.binder.OrderEmptyBinder;
import com.one.efaimaly.order.model.extra.UploadImgExtra;
import com.tencent.map.geolocation.TencentLocation;

import static com.one.common.config.OrderState.PAY_END;
import static com.one.common.config.OrderState.WAIT_PAY;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 上午9:21
 * description ：首页
 **/
public class HomeFragment extends BaseListFragment<MainPresenter> implements IListView,
        BannerBinder.OnCreateBanner,
        View.OnClickListener,
        LocationUtils.OnLocationListener,
        OrderBinder.OrderBtnClickListener{
    private ConvenientBanner convenientBanner;
    private TextView tvCity;
    private LatLng meLatlng;
    private String starName;
    @Override
    protected void initPresenter() {
        mPresenter = new MainPresenter(this,getActivity());
    }

    @Override
    public void registerDate() {
        register(BannerItem.class, new BannerBinder(this));
        register(OrderItem.class, new OrderBinder(this));
        register(OrderEmptyItem.class, new OrderEmptyBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getData(OrderState.ALL);
    }

    @Override
    public void initView() {
        super.initView();
        addTopView(R.layout.layout_home_top);
        defaultItem();
        tvCity = successView.findViewById(R.id.tv_city);
        tvCity.setOnClickListener(this);
    }

    private void defaultItem(){
//        addData(new BannerItem());
//        addData(new OrderItem());
//        addData(new OrderItem());
//        addData(new OrderItem());
    }

    @Override
    public void initData() {
        super.initData();
        // 调用定位
        LocationUtils.location(getActivity(), this);
//        mPresenter.getData(OrderState.ALL);
    }

    @Override
    public void onResume() {
        super.onResume();
       doRefresh();
        if (convenientBanner != null && !convenientBanner.isTurning()) {
            convenientBanner.startTurning(2000);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        if (convenientBanner != null) {
            convenientBanner.stopTurning();
        }
    }

    @Override
    public boolean onEmpty() {
        addData(new OrderEmptyItem());
        return true;
    }

    @Override
    public void onCreateView(ConvenientBanner convenientBanner) {
        this.convenientBanner = convenientBanner;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_city:
                RouterManager.getInstance().goForResult(getActivity(), RouterPath.SELECT_CITY);
                break;
        }
    }

    @Subscribe
    public void onSelectCity(SelectCityEvent event){
        if(StringUtils.isNotBlank(event.getCity())){
            tvCity.setText(event.getCity());
        }
    }

    @Override
    public void onLocated(TencentLocation aMapLocation) {
        if(aMapLocation!=null && StringUtils.isNotBlank(aMapLocation.getCity())){
            tvCity.setText(aMapLocation.getCity());
            starName = aMapLocation.getAddress();
            meLatlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        }
    }

    @Override
    public void onLocationPermissionFail() {
        Toaster.showLongToast(StringUtils.format(R.string.permission_fail_local), MyToast.FAIL);
    }


    @Override
    public void onNavigation(OrderItem item) {
        if (item != null && item.getMaintailOrderAddressBean() != null) {
            MapUtils.selectMap(getActivity(), meLatlng, starName,
                    item.getMaintailOrderAddressBean().getLatLng(),
                    item.getMaintailOrderAddressBean().getAddress());
        }
        //RouterManager.getInstance().go(RouterPath.UPLOAD_IMG);
    }

    @Override
    public void onCancel(OrderItem item) {
        if(item.getOrder_state().equals(PAY_END)){
            if(StringUtils.isBlank(item.getOrder_maintail_imgs1()) &&
                    StringUtils.isBlank(item.getOrder_maintail_imgs2())){
                // 上传图片
                RouterManager.getInstance().go(RouterPath.UPLOAD_IMG, new UploadImgExtra(item.getOrder_id()+""));
            } else {
                // 查看图片
                RouterManager.getInstance().go(RouterPath.UPLOAD_IMG,
                        new UploadImgExtra(item.getOrder_maintail_imgs1(),
                                item.getOrder_maintail_imgs2(),item.getOrder_id()+""));
            }
        } else { // 取消订单
            mPresenter.cancel(item.getOrder_id()+"");
        }
    }
}
